/**
 * fileName: DatePicker.java
 *     Date: 2012-8-5
 **/
package com.stranger.common.tag.datepicker;

import java.util.UUID;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * @作者 stranger.lin
 *
 * @时间 2012-8-5
 */
public class DatePicker extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String name;
	private String id;
	private String value;
	private String className;
	private String style;
	private String readOnly;
	private JspWriter out;
	
	@Override
	public int doStartTag() throws JspException {
		try {
			if (out==null) {
				out=pageContext.getOut();	
			}
			if (id==null||"".equals(id.trim())) {
				id="_datepicker_"+UUID.randomUUID();
			}
			StringBuffer html=new StringBuffer("<input type=\"text\" id=\""+id+"\"  ");
			if(name!=null&&!"".equals(name.trim())) html.append(" name=\""+name+"\"");
			if (value!=null)html.append("  value=\""+value+"\"");
			if (className!=null&&!"".equals(className.trim()))
				html.append(" calss=\""+className+"\"");
			if (style!=null&&!"".equals(style.trim()))
				html.append(" style=\""+style+"\"");
			if (readOnly!=null&&!"".equals(readOnly))
				html.append(" readonly=\""+readOnly+"\"");
			html.append("/>");
			out.print(html);
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
			throw new JspException(e.getMessage());
		}
		
		return EVAL_PAGE;
	}
	@Override
	public int doEndTag() throws JspException {
		try {
			if (out==null) {
				out=pageContext.getOut();	
			}
			StringBuffer js=new StringBuffer("<script>$(function() {\n");
			js.append("$( \"#"+id+"\" ).datepicker({\n");
			js.append("changeMonth: true,changeYear: true,showOn: \"button\"," +
					"buttonImage: \"images/datepicker/calendar.gif\",buttonImageOnly: true});});</script>");
			out.print(js);
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
			throw new JspException(e.getMessage());
		}
		return EVAL_PAGE;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	public String getStyle() {
		return style;
	}
	public void setStyle(String style) {
		this.style = style;
	}
	public String getReadOnly() {
		return readOnly;
	}
	public void setReadOnly(String readOnly) {
		this.readOnly = readOnly;
	}
	
	
	
}
